<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import { defaultRoleForm, rules } from '../index'
import type { CustomDialogInstance } from '@/types'
// ------------------use------------------
// #region
defineProps<{
  title: string
}>()
const emits = defineEmits<{
  'save-success': []
}>()
const formData = defineModel('data', {
  default: () => defaultRoleForm,
})

const dialogRef = ref<CustomDialogInstance | null>(null)
const formRef = ref<FormInstance | null>(null)

async function open() {
  dialogRef.value?.open?.()
  await nextTick()
  dialogRef.value?.setSaving?.(false)
  formRef.value?.resetFields()
}
function close() {
  dialogRef.value?.close?.()
}
async function clickSave(done: () => void) {
  try {
    await formRef.value?.validate()
    emits('save-success')
  }
  catch (error) {
    done()
  }
}
// #endregion
defineExpose({
  open,
  close,
})
</script>

<template>
  <CustomDialog
    ref="dialogRef"
    :title="title"
    width="600px"
    @confirm="clickSave"
  >
    <el-form
      ref="formRef"
      v-loading
      label-width="80px"
      label-position="right"
      :model="formData"
      :rules="rules"
      inline
    >
      <el-form-item
        label="角色名称"
        prop="roleName"
        class="width-fill"
      >
        <el-input
          v-model.trim="formData.roleName"
          maxlength="20"
          placeholder="请输入角色名称"
        />
      </el-form-item>
    </el-form>
  </CustomDialog>
</template>

<style lang="scss" scoped>
//------------------组件样式------------------
//-------------样式穿透-------------
</style>
